//package com.boot.down.config;
//
//import org.apache.commons.dbcp2.BasicDataSourceFactory;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.mybatis.spring.mapper.MapperScannerConfigurer;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.ComponentScan;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.FilterType;
//import org.springframework.core.io.ClassPathResource;
//import org.springframework.core.io.Resource;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//import org.springframework.stereotype.Repository;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.PlatformTransactionManager;
//import org.springframework.transaction.annotation.EnableTransactionManagement;
//import org.springframework.transaction.annotation.TransactionManagementConfigurer;
//
//import javax.sql.DataSource;
//import java.util.Properties;
//
//@Configuration
///** 定义扫描包*/
//@ComponentScan(value = "com.*", includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Service.class})})
///** 使用事物驱动管理器*/
//@EnableTransactionManagement
//public class RootConfig implements TransactionManagementConfigurer {
//
//    private DataSource dataSource = null;
//
//    /**
//     * 创建连接池
//     * @return
//     */
//    @Bean(name = "dataSource")
//    public DataSource initDataSource(){
//        if(dataSource != null){
//            return dataSource;
//        }
//        Properties props = new Properties();
//        props.setProperty("driverClassName","com.mysql.jdbc.Driver");
//        props.setProperty("url","jdbc:mysql://localhost:3306/you_boot?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
//        props.setProperty("username","root");
//        props.setProperty("password", "123456");
//        props.setProperty("maxActive", "200");
//        props.setProperty("maxIdle", "20");
//        props.setProperty("maxWait", "30000");
//        try {
//            dataSource = BasicDataSourceFactory.createDataSource(props);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return dataSource;
//    }
//
//    /**
//     * 创建sqlSessionFactoryBean
//     * @return
//     */
//    @Bean(name = "sqlSessionFactory")
//    public SqlSessionFactoryBean initSqlSessionFactory(){
//        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
//        sqlSessionFactory.setDataSource(initDataSource());
//        Resource resource = new ClassPathResource("mybatis/mybatis-config.xml");
//        sqlSessionFactory.setConfigLocation(resource);
//        return sqlSessionFactory;
//    }
//
//    /**
//     * 扫描mapper
//     * @return
//     */
//    @Bean
//    public MapperScannerConfigurer initMapperScannerConfigurer(){
//
//        MapperScannerConfigurer msc = new MapperScannerConfigurer();
//        msc.setBasePackage("com.*");
//        msc.setSqlSessionFactoryBeanName("sqlSessionFactory");
//        msc.setAnnotationClass(Repository.class);
//
//        return msc;
//    }
//
//    /**
//     * 实现接口方法，注册注解事物
//     * @return
//     */
//    @Override
//    public PlatformTransactionManager annotationDrivenTransactionManager() {
//
//        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
//        transactionManager.setDataSource(initDataSource());
//        return transactionManager;
//    }
//}
